package com.lnlyj.media.base;

import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lnlyj on 2019/6/27.
 */

public abstract class BaseGroupAdapter<Group, GroupVH extends RecyclerView.ViewHolder, ChildVH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter {

    public static final int INVALID_POSITION = -1;

    public static final int TYPE_GROUP = 1;
    public static final int TYPE_CHILD = 2;

    private boolean hasTitle = false;

    private List<Group> groups = new ArrayList<>();

    private int mItemCount;

    public BaseGroupAdapter(boolean hasTitle) {
        this.hasTitle = hasTitle;
    }

    @Override
    public int getItemViewType(int position) {

        if (!hasTitle) {
            return TYPE_CHILD;
        }

        return getItemType(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

        if (viewType == TYPE_GROUP) {
            return onCreateGroupViewHolder(viewGroup);
        } else if (viewType == TYPE_CHILD) {
            return onCreateChildViewHolder(viewGroup);
        }

        return onCreateChildViewHolder(viewGroup);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int index) {
        Position position = getGroupChildPosition(index);
        if (position.child == INVALID_POSITION) {
            onBindGroup((GroupVH) holder, position.group);
        } else {
            onBindChild((ChildVH) holder, position.group, position.child);
        }
    }

    @Override
    public int getItemCount() {
        return mItemCount;
    }

    protected abstract GroupVH onCreateGroupViewHolder(ViewGroup parent);
    protected abstract ChildVH onCreateChildViewHolder(ViewGroup parent);
    public abstract void onBindGroup(GroupVH holder, int groupPosition);
    public abstract void onBindChild(ChildVH holder, int groupPosition, int childPosition);


    private void updateItemCount() {
        int count = 0;
        int g = !hasTitle ? 0 : 1;
        for (Group group : groups) {
            count += getChildCount(group) + g;
        }
        mItemCount = count;
    }

    public Group getGroup(int position) {
        return groups.get(position);
    }

    public List<Group> getGroups() {
        return groups;
    }

    public int getGroupCount() {
        return groups == null ? 0 : groups.size();
    }

    public abstract int getChildCount(Group group);

    public Position getGroupChildPosition(int itemPosition) {
        int itemCount = 0;
        int childCount;
        final Position position = new Position();
        for (Group g : groups) {
            if (hasTitle) {
                if (itemPosition == itemCount) {
                    position.child = INVALID_POSITION;
                    return position;
                }
                itemCount++;
            }
            childCount = getChildCount(g);
            if (childCount > 0) {
                position.child = itemPosition - itemCount;
                if (position.child < childCount) {
                    return position;
                }
                itemCount += childCount;
            }
            position.group++;
        }
        return position;
    }

    public int getItemType(final int itemPosition) {
        int count = 0;
        int childCount;
        for (Group g : groups) {
            if (itemPosition == count) {
                return TYPE_GROUP;
            }
            childCount = getChildCount(g);
            count += 1;
            if (itemPosition == count && childCount != 0) {
                return TYPE_CHILD;
            }
            count += childCount;
            if (itemPosition < count) {
                return TYPE_CHILD;
            }
        }
        throw new IllegalStateException("invalid position = " + itemPosition);
    }

    public void swapResult(List<Group> result) {
        groups.clear();
        if (result != null && !result.isEmpty()) {
            groups.addAll(result);
        }
        updateItemCount();
        notifyDataSetChanged();
    }

    public int getRealItemCount() {
        return hasTitle ? mItemCount - groups.size() : mItemCount;
    }

    public static class Position {
        public int group;
        public int child = INVALID_POSITION;
    }
}
